import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
    children: [
      // 产品管理
      {
        path: '/product/create',
        name: 'ProductCreate',
        component: () => import('../views/Products/Create.vue')
      },
      {
        path: '/product/edit/:id',
        name: 'ProductEdit',
        props: true,
        component: () => import('../views/Products/Create.vue')
      },
      {
        path: '/product/list',
        name: 'ProductList',
        component: () => import('../views/Products/List.vue')
      },
      // 订单管理
      {
        path: '/order/list',
        name: 'OrderList',
        component: () => import('../views/Orders/List.vue')
      },
      {
        path: '/order/items',
        name: 'OrderItems',
        component: () => import('../views/Orders/items.vue')
      },
      {
        path: '/swiper',
        name: 'Swiper',
        component: () => import('../views/Orders/Swiper.vue')
      },
      // 人员管理
      {
        path: '/user',
        name: 'UserList',
        component: () => import('../views/Users/User.vue')
      },
      // 地址管理
      {
        path: '/address/create',
        name: 'AddressCreate',
        component: () => import('../views/Address/Edit.vue')
      },
      {
        path: '/address/edit/:id',
        name: 'AddressEdit',
        props: true,
        component: () => import('../views/Address/Edit.vue')
      },
      {
        path: '/address/list',
        name: 'AddressList',
        component: () => import('../views/Address/List.vue')
      },
    ]
  },
  {
    path:'/login',
    name: 'Login',
    meta: {
      isPublic: true
    },
    component:()=>import('../views/Login/Login.vue')
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !sessionStorage.token) {
    return next("/login");
  }
  next();
});

export default router
